<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/*友情连接*/
class Link extends MY_Controller {
	public function __construct(){
		parent::__construct();
		//权限处理
		$this->sess_checked();
		$this->load->model('linkclass_m');
		$this->load->model('link_m');
	}
	//友情连接分类
	public function classlists(){
		$where=array();
        $res=$this->linkclass_m->ajaxall($where);
        $this->assign('list',$res['list']);
        if(isset($_POST['ajaxs'])){
            $htmlcontent=$this->fetch('link/classlistsajaxs.html');
            $res['list']=$htmlcontent;
            ajax_return(1,'',$res);
        }
        $this->assign('pageshow',$res['pageshow']);
		$this->display('link/classlists.html');
	}
	//添加友情连接分类
	public function classadd(){
		 if(isset($_POST['ajaxs'])){
            $data=form_ajaxs($_POST['formjquery']);
            if(!isset($data['classname']) || $data['classname']=='' ){
            	ajax_return(0,'分类名称不能为空');
            }
            $where['classname']=array('=',$data['classname']);
            if($list=$this->linkclass_m->sel($where,'classname')){
                ajax_return(0,'已经存在分类名称'.$data['classname']);
            }
            $data['addtime']=time();
            if($addid=$this->linkclass_m->add($data)){
                $this->adminuserRecord_m->add(1,1,'友情连接分类classid为'.$addid);
                ajax_return(1,'添加友情连接分类成功');
            }else{
                $this->adminuserRecord_m->add(1,2,'友情连接分类');
                ajax_return(0,'添加友情连接分类失败');
            }
        }else{
           $this->display('link/classadd.html');
        }
	}
	//编辑友情连接分类
	public function classedit(){
		 if(isset($_POST['ajaxs'])){
            $data=form_ajaxs($_POST['formjquery']);
            if(!isset($data['classname']) || $data['classname']=='' ){
            	ajax_return(0,'分类名称不能为空');
            }
            $where['classid']=array('!=',$data['classid']);
            $where['classname']=array('=',$data['classname']);
            if($list=$this->linkclass_m->sel($where,'classname')){
                ajax_return(0,'已经存在分类名称'.$data['classname']);
            }
            $whereedit['classid']=array('=',$data['classid']);
            if($addid=$this->linkclass_m->edit($whereedit,$data)){
                $this->adminuserRecord_m->add(2,1,'友情连接分类classid为'.$data['classid']);
                ajax_return(1,'编辑友情连接分类成功');
            }else{
                $this->adminuserRecord_m->add(2,2,'友情连接分类');
                ajax_return(0,'编辑友情连接分类失败');
            }
        }elseif(isset($_GET['id'])){
    		$where['classid']=array('=',$_GET['id']);
    		$list=$this->linkclass_m->sel($where);
    		$this->assign('list',$list);
           	$this->display('link/classedit.html');
        }
	}
	//删除友情连接分类
	public function classdel(){
		if(isset($_POST['classid'])){
            $classid=$_POST['classid'];
            $where['classid']=array('=',$classid);
        }else{
            ajax_return(0,'参数出错');
        }
        //判断是否存在友情分类
        if($this->link_m->sel($where)){
        	ajax_return(0,'存在友情连接数据,不能删除');
        }

        if($this->linkclass_m->del($where)===false){
            $this->adminuserRecord_m->add(3,2,'友情链接分类classid为'.$classid);
            ajax_return(0,'删除失败');
        }
        $this->adminuserRecord_m->add(3,1,'友情链接分类classid为'.$classid);
        ajax_return(1,'删除成功');
	}
	//友情连接列表
	public function lists(){
		$where=array();
        if(isset($_POST['classid'])){
            $where['A.classid']=array('=',$_POST['classid']);
        }
        if(isset($_POST['type'])){
            $where['A.type']=array('=',$_POST['type']);
        }
        $res=$this->link_m->ajaxall($where);
        $this->assign('list',$res['list']);
        if(isset($_POST['ajaxs'])){
            $htmlcontent=$this->fetch('link/listsajaxs.html');
            $res['list']=$htmlcontent;
            ajax_return(1,'',$res);
        }
        $this->assign('pageshow',$res['pageshow']);
        //获取友情连接分类
        $linkclass=$this->linkclass_m->selall();
        $this->assign('linkclass',$linkclass);
		$this->display('link/lists.html');
	}
	public function add(){
		if(isset($_POST['ajaxs'])){
            $data=form_ajaxs($_POST['formjquery']);
            if(!isset($data['sitename']) || $data['sitename']=='' ){
            	ajax_return(0,'网站名称不能为空');
            }
			if(!isset($data['url']) || $data['url']=='' ){
            	ajax_return(0,'URL不能为空');
            }
			
			if($data['type']==0){
				if(!isset($data['logo']) || $data['logo']=='' ){ajax_return(0,'请上传logo');}
			}
            $where['sitename']=array('=',$data['sitename']);
            if($list=$this->link_m->sel($where,'sitename')){
                ajax_return(0,'网站名称已经存在'.$data['sitename']);
            }
            $data['addtime']=time();
			$data['hits']=0;
            if($addid=$this->link_m->add($data)){
                $this->adminuserRecord_m->add(1,1,'友情连接为id'.$addid);
                ajax_return(1,'添加友情连接成功');
            }else{
                $this->adminuserRecord_m->add(1,2,'友情连接');
                ajax_return(0,'添加友情连接失败');
            }
        }else{
			//获取友情连接分类
			$linkclass=$this->linkclass_m->selall();
			$this->assign('linkclass',$linkclass);
            $this->display('link/add.html');
			
        }
	}
    //编辑友情连接
    public function edit(){
		if(isset($_POST['ajaxs']) && isset($_POST['protrudingstatus'])){
			$field_id=explode('_',$_POST['field_id']);
			$where['id']=array('=',$field_id[1]);
			$data[$field_id[0]]=$_POST['value'];
			if($field_id[0]=="recommend"){$msg='推荐';}else{$msg="审核";}
			if($_POST['value']==1){$editmsg="开启";}else{$editmsg="关闭";}
			if($this->link_m->edit($where,$data)!==false){
                $this->adminuserRecord_m->add(2,1,'友情连接id为'.$field_id[1].$msg.$editmsg);
                ajax_return(1,$msg.$editmsg."成功");
            }else{
                $this->adminuserRecord_m->add(2,2,'友情连接id为'.$field_id[1].$msg.$editmsg);
                ajax_return(0,$msg.$editmsg.'失败');
            }
		}else if(isset($_POST['editorderid'])){//排序
            $where['id']=array('=',$_POST['id']);
            $data['orderid']=$_POST['orderid'];
            if($this->link_m->edit($where,$data)!==false){
                $this->adminuserRecord_m->add(2,1,'友情连接id为'.$_POST['id']."修改排序为".$_POST['orderid']);
                ajax_return(1,"修改排序成功");
            }else{
                $this->adminuserRecord_m->add(2,2,'友情连接id为'.$_POST['id']."修改排序为".$_POST['orderid']);
                ajax_return(0,'修改排序失败');
            }
        }elseif(isset($_POST['ajaxs'])){
             $data=form_ajaxs($_POST['formjquery']);
             if(!isset($data['sitename']) || $data['sitename']=='' ){
                    ajax_return(0,'网站名称不能为空');
                }
            if(!isset($data['url']) || $data['url']=='' ){
                ajax_return(0,'URL不能为空');
            }
            
            if($data['type']==0){
                if(!isset($data['logo']) || $data['logo']=='' ){ajax_return(0,'请上传logo');}
            }
            $where['id']=array('!=',$data['id']);
            $where['sitename']=array('=',$data['sitename']);
            if($list=$this->link_m->sel($where,'sitename')){
                ajax_return(0,'网站名称已经存在'.$data['sitename']);
            }
            $where_e['id']=array('=',$data['id']);
            if($addid=$this->link_m->edit($where_e,$data)){
                $this->adminuserRecord_m->add(2,1,'友情连接为id'.$addid);
                ajax_return(1,'编辑友情连接成功');
            }else{
                $this->adminuserRecord_m->add(2,2,'友情连接');
                ajax_return(0,'编辑友情连接失败');
            }

        }elseif(isset($_GET['id'])){//编辑内容
            //获取友情连接分类
            $linkclass=$this->linkclass_m->selall();
            $this->assign('linkclass',$linkclass);

            $where['id']=array('=',$_GET['id']);
            $list=$this->link_m->sel($where);
            $this->assign('list',$list);
            $this->display('link/edit.html');
        }
    }

}
